local skynet = require "skynet"
local proxypack = require "proxypack"
local msgdef = require "msgdef"
local utils = require "utils"
local util  = require "util"
local pb = require 'protobuf'
local httpc = require "http.httpc"
local cjson = require "cjson"
local error_code = require "error_code"
local sharedata     = require "skynet.sharedata"
local hosts = require "hosts"
local constant = require "constant"
local server_config = dofile(skynet.getenv("server_config"))
-- httpc.timeout = 10 * 100 --ten seconds.

-- local ACCOUNT_WEBSERVER_URL = hosts.resolve(skynet.getenv("ACCOUNT_WEBSERVER_URL") or "127.0.0.1:6668")
-- local ACCOUNT_PAY_WEBSERVER_URL = hosts.resolve(skynet.getenv("ACCOUNT_PAY_WEBSERVER_URL") or "127.0.0.1:6669")
local DEBUG = server_config.debug
local client_fd
local client_ip
local client_ip_num

local account = {}
local global_configs
-- if DEBUG then
-- function account.REQ_VERIFY(msg)
--     local uid = tonumber(msg.uid)

--     local rsp = {
--         result = 0,
--         uid = uid,
--         key_token = msg.key_token
--     }

--     utils.send_to_gateway(0,uid,client_fd,'account.RSP_VERIFY',rsp,uid,1)
-- end
-- else



--检验token
function account.REQ_VERIFY(msg)
    local matchine_token = msg.matchine_token
    local package_channel = msg.package_channel
    if #matchine_token == 0 then
        matchine_token = nil
    end

    local body = cjson.encode({
        uid = msg.uid,
        key_token = msg.key_token,
        matchine_token = matchine_token,
        client_ip = client_ip,
        package_channel = package_channel,
        os_system = msg.os_system,
        platform = msg.platform,
    })
    local exheader = {
        uid = msg.uid,
        ip = client_ip_num,
        channel = package_channel,
        os_system = msg.os_system,
        platform = msg.platform,
    }
    local status, body = skynet.call('.web_proxy','lua','web_post',constant.GET_TOKEN_URL,body, exheader)
    -- local status, body = httpc.request('POST',ACCOUNT_WEBSERVER_URL, 
    --     "/starry/user/check_token",nil,nil,body)
    if status ~= 200 then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY',
            {result = error_code.VERIFY_ERROR_RETRY})
    end
    
    local result = cjson.decode(body)
    if result.code ~= 0 then
        local ret = error_code.VERIFY_ERROR_RETRY
        if result.code == -100 then
            ret = error_code.EXPIRED_LOGIN
        end

        if result.code == -118 or result.code == -119 or result.code == -120 then
            print("REQ_VERIFY---> ip or matchine limit ",result.code)
            return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY',
                {result = error_code.RESTRICT_ERROR})
        else
            return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY',
                {result = ret,channel = result.channel})
        end
    end

    if not result.account_id or not result.key_token then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY',
            {result = error_code.VERIFY_ERROR_RETRY,channel = result.channel})
    end

    dbglog(tostring_r(result))
    local account_id = tonumber(result.account_id)
    local uid = skynet.call('.guid_mgr','lua','get_uid',account_id)
    if not uid then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY',{result = error_code.TOURIST_REGISTER_FAIL})
    end

    return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY',{
        uid = uid,key_token = result.key_token, channel = result.channel,url = result.url,real_channel = result.pkg_channel},uid,account_id)
end
-- end

--游客注册
function account.REQ_REGISTER_TOURIST(msg)

    local matchine_token = msg.matchine_token
    local package_channel = msg.package_channel
    local os_system = msg.os_system
    local platform = msg.platform
    local body = cjson.encode({
        matchine_token = matchine_token,
        client_ip = client_ip,
        package_channel = package_channel,
        os_system = os_system,
        platform = platform,
    })
    local exheader = {
        -- uid = uid,
        ip = client_ip_num,
        channel = package_channel,
        -- client_version = client_version,
        -- device_brand = device_brand,
        -- device_id = device_id,
        -- account_id = account_id,
        os_system = os_system,
        platform = platform,
        -- current_system = current_system,
    }    
    print("___REQ_REGISTER_TOURIST____", msg, body, exheader)
    local status, body = skynet.call('.web_proxy','lua','web_post',constant.REGISTER_TOURIST,body, exheader)
    -- local status, body = httpc.request('POST',ACCOUNT_WEBSERVER_URL, 
    --     "/starry/user/register_tourist",nil,nil,body)

    if status ~= 200 then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_REGISTER_TOURIST',
            {result = error_code.TOURIST_REGISTER_FAIL})
    end

    local result = cjson.decode(body)
    --dbglog(tostring_r(result))
    if result.code ~= 0 then
        if result.code == -118 or result.code == -119 or result.code == -120 then
            print("REQ_REGISTER_TOURIST---> ip or matchine limit ",result.code)
            return utils.send_to_gateway(0,-1,client_fd,'account.RSP_REGISTER_TOURIST',
                {result = error_code.RESTRICT_ERROR})
        else
            return utils.send_to_gateway(0,-1,client_fd,'account.RSP_REGISTER_TOURIST',
                {result = error_code.TOURIST_REGISTER_FAIL})
        end
    end

    if not result.account_id or not result.key_token then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_REGISTER_TOURIST',
            {result = error_code.TOURIST_REGISTER_FAIL})
    end
    --dbglog(tostring_r(result))
    local account_id = result.account_id
    local uid = skynet.call('.guid_mgr','lua','get_uid',account_id)
    if not uid then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_REGISTER_TOURIST',{result = error_code.TOURIST_REGISTER_FAIL})
    end
    return utils.send_to_gateway(0,-1,client_fd,'account.RSP_REGISTER_TOURIST',{
        uid = uid,key_token = result.key_token,real_channel = result.pkg_channel,url = result.url},uid,account_id)
end

--取验证码
function account.REQ_GET_INVITATION_CODE(msg)
    local phone_number = msg.phone_number
    local tourist_key_token = msg.tourist_key_token
    local is_bind = msg.is_bind
    local package_channel = msg.package_channel
    if #tourist_key_token == 0 then
        tourist_key_token = nil
    end

    local body = cjson.encode({
            phone_number = phone_number,
            tourist_key_token = tourist_key_token,
            is_bind = is_bind,
            package_channel = package_channel,
            client_ip = client_ip,
        })
    local exheader = {
        -- uid = uid,
        ip = client_ip_num,
        channel = package_channel,
        -- client_version = client_version,
        -- device_brand = device_brand,
        -- device_id = device_id,
        -- account_id = account_id,
        -- os_system = os_system,
        -- platform = platform,
        -- current_system = current_system,
    }  

    local status, body = skynet.call('.web_proxy','lua','web_post',constant.GET_INVITATION_CODE, body, exheader)
    -- local status, body = httpc.request('POST',ACCOUNT_WEBSERVER_URL, 
    --     "/starry/user/get_invitation_code",nil,nil,body)
    if status ~= 200 then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_GET_INVITATION_CODE',
            {result = error_code.PHONE_REGISTER_FAIL})
    end

    local result = cjson.decode(body)
    if result.code ~= 0 then
        local ret = error_code.PHONE_REGISTER_FAIL
        if result.code == -117 then
            ret = error_code.PHONE_IS_REGISTED
        elseif result.code == -112 then
            ret = error_code.WAIT_TO_VERIFY
        elseif result.code == -103 then
            ret = error_code.ERROR_TOURIST_NO_FOUND
        end
        dbglog("RSP_GET_INVITATION_CODE,",result.code)
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_GET_INVITATION_CODE',{result = ret})
    end

    --验证码已发送
    return utils.send_to_gateway(0,-1,client_fd,'account.RSP_GET_INVITATION_CODE',{})
end

--校验手机码登录（已废弃
function account.REQ_VERIFY_INVITATION_CODE(msg)
    local phone_number = msg.phone_number
    local code = msg.code
    local matchine_token = msg.matchine_token
    local check_way = msg.check_way
    
    if #matchine_token == 0 then
        matchine_token = nil
    end
    dbglog("REQ_VERIFY_INVITATION_CODE",msg.phone_number)
    local body = cjson.encode({
            phone_number = phone_number,
            code = code,
            matchine_token = matchine_token,
            client_ip = client_ip,
            check_way = check_way,
        })
    local exheader = {
        ip = client_ip_num,
    }      
    local status, body = skynet.call('.web_proxy','lua','web_post',constant.VERIFY_REGISTER_INVITATION_CODE,body, exheader)
    -- local status, body = httpc.request('POST',ACCOUNT_WEBSERVER_URL, 
    --     "/starry/user/verify_register_invitation_code",nil,nil,body)

    if status ~= 200 then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY_INVITATION_CODE',
            {result = error_code.VERIFY_ERROR_RETRY})
    end

    local result = cjson.decode(body)
    if result.code ~= 0 then
        if result.code == -118 or result.code == -119 or result.code == -120 then
            print("REQ_VERIFY_INVITATION_CODE---> ip or matchine limit ",result.code)
            return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY_INVITATION_CODE',
                {result = error_code.RESTRICT_ERROR})
        else
            return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY_INVITATION_CODE',
                {result = error_code.VERIFY_CODE_EXCEPT})
        end
    end

    local account_id = result.account_id 
    local uid = skynet.call('.guid_mgr','lua','get_uid',account_id)
    if not uid or not result.key_token then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY_INVITATION_CODE',
            {result = -3})
    end

    dbglog("REQ_VERIFY_INVITATION_CODE",result.bind,uid)
    if result.bind and uid then
        billlog({op="player_bind_phone",uid = uid,phone = phone_number})
        
    end

    return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY_INVITATION_CODE',
        {uid = uid,key_token = result.key_token,
        tourist_key_token = result.tourist_key_token,url = result.url,real_channel = result.pkg_channel,check_way = check_way},uid,account_id)
end

--登录验证码
function account.REQ_GET_LOGIN_CODE(msg)
    local phone_number = msg.phone_number
    local exheader = {
        ip = client_ip_num,
    }      
    local body = cjson.encode({phone_number = phone_number})
    local status, body = skynet.call('.web_proxy','lua','web_post',constant.GET_LOGIN_RAND_CODE, body, exheader)
    -- local status, body = httpc.request('POST',ACCOUNT_WEBSERVER_URL, 
    --     "/starry/user/get_login_rand_code",nil,nil,body)
    if status ~= 200 then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_GET_LOGIN_CODE',
            {result = error_code.VERIFY_ERROR_RETRY})
    end

    local result = cjson.decode(body)
    if result.code ~= 0 then
        local ret = error_code.GET_VERIFY_CODE_FAIL
        if result.code == -31 then
            ret = error_code.VERIFY_IS_NOT_EXIST
        elseif result.code == -32 then
            ret = error_code.WAIT_TO_VERIFY
        end
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_GET_LOGIN_CODE',{result = ret})
    end

    --验证码已发送
    return utils.send_to_gateway(0,-1,client_fd,'account.RSP_GET_LOGIN_CODE',{result = 0})
end

-- function account.REQ_VERIFY_LOGIN_CODE(msg)
--     local phone_number = msg.phone_number
--     local code = msg.code

--     local body = cjson.encode({phone_number = phone_number,code = code})
--     local status, body = httpc.request('POST',ACCOUNT_WEBSERVER_URL, 
--         "/starry/user/verify_login_rand_code",nil,nil,body)

--     if status ~= 200 then
--         return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY_LOGIN_CODE',
--             {result = error_code.VERIFY_ERROR_RETRY})
--     end

--     local result = cjson.decode(body)
--     if result.code ~= 0 then
--         return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY_LOGIN_CODE',
--             {result = error_code.VERIFY_CODE_EXCEPT})
--     end

--     if not result.uid or not result.key_token then
--         return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY_LOGIN_CODE',{result = -3})
--     end

--     local uid = result.uid
--     return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY_LOGIN_CODE',
--         {uid = uid,key_token = result.key_token},uid)
-- end

--第三方登录
function account.REQ_VERIFY_3RD_LOGIN(msg)
    local code = msg.code
    local channel = msg.channel
    local uid = msg.uid
    local exheader = {
        ip = client_ip_num,
    }  
    local body = cjson.encode({code = code,channel = channel,uid = uid})
    local status, body = skynet.call('.web_proxy','lua','web_post',constant.VERIFY_3RD_LOGIN,body, exheader)
    -- local status, body = httpc.request('POST',ACCOUNT_WEBSERVER_URL, 
    --     "/starry/user/verify_3rd_login",nil,nil,body)

    if status ~= 200 then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY_3RD_LOGIN',
            {result = error_code.VERIFY_ERROR_RETRY})
    end

    local result = cjson.decode(body)
    print("REQ_VERIFY_3RD_LOGIN====",tostring_r(result))
    if result.code ~= 0 then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY_3RD_LOGIN',
            {result = error_code.VERIFY_CODE_EXCEPT})
    end

    if not result.account_id or not result.key_token then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY_3RD_LOGIN',
            {result = error_code.VERIFY_ERROR_RETRY})
    end

    local account_id = result.account_id 
    local uid = skynet.call('.guid_mgr','lua','get_uid',account_id)
    if not uid then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_REGISTER_TOURIST',{result = error_code.TOURIST_REGISTER_FAIL})
    end

    return utils.send_to_gateway(0,-1,client_fd,'account.RSP_VERIFY_3RD_LOGIN',
        {uid = uid,key_token = result.key_token,channel = channel},uid,account_id)
end

--支付方式
function account.REQ_MAKE_ORDER(msg)
    local product_id = msg.product_id
    local channel = msg.channel
    local uid = msg.uid
    local payment_channel = msg.payment_channel
    local payment_way = msg.payment_way
    local pay_amount  = msg.pay_amount
    local cur_channel = msg.cur_channel
    local platform = msg.platform
    local app_name = msg.app_name
    local app_id   = msg.app_id

    local body = cjson.encode({
        product_id = product_id,
        channel = channel,
        cur_channel = cur_channel,
        uid = uid,
        payment_channel = payment_channel,
        payment_way = payment_way,
        pay_amount = pay_amount,
        platform = platform,
        app_name = app_name,
        app_id = app_id,
    })
    local exheader = {
        ip = client_ip_num,
    }  
    local status, body = skynet.call('.web_proxy','lua','web_post',constant.MAKE_ORDER,body, exheader)
    -- local status, body = httpc.request('POST',ACCOUNT_PAY_WEBSERVER_URL, 
    --     "/starry/order/make_order",nil,nil,body)

    if status ~= 200 then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_MAKE_ORDER',{
            result = -1,
            payment_channel = payment_channel,
            payment_way = payment_way,
        })
    end

    local result = cjson.decode(body)
    if result.code ~= 0 then
        errlog(uid,'failed to make order',result.reason)
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_MAKE_ORDER',{
            result = -2,
            payment_channel = payment_channel,
            payment_way = payment_way,
        })
    end

    return utils.send_to_gateway(0,-1,client_fd,'account.RSP_MAKE_ORDER',{
        data = result.data,
        payment_channel = payment_channel,
        payment_way = payment_way,
    })
end

function account.REQ_CHECK_PAYMENT(msg)
    local receipt = msg.receipt
    local order_id = msg.order_id
    local exheader = {
        ip = client_ip_num,
    }  
    local body = cjson.encode({receipt = receipt,order_id = order_id})
    local status, body = skynet.call('.web_proxy','lua','web_post',constant.CHECK_PAYMENT,body, exheader)
    -- local status, body = httpc.request('POST',ACCOUNT_PAY_WEBSERVER_URL, 
    --     "/starry/order/check_payment",nil,nil,body)

    if status ~= 200 then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_CHECK_PAYMENT',{result = -1})
    end

    local result = cjson.decode(body)
    if result.code ~= 0 then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_CHECK_PAYMENT',{result = -2})
    end

    local uid = result.uid
    return utils.send_to_gateway(0,-1,client_fd,'account.RSP_CHECK_PAYMENT',{})
end





--2020-03-06
-- const ERROR_TOKEN_PWD = -121; //账号或密码不正确
-- const ERROR_PWD = -122; //密码只能输入6~20位，只能数字和英文，且要包含数字和英文
-- const ERROR_PWD_DIFF = -122; //两次输入的密码不一致
-- const ERROR_PHONE_INVAILD = -123; //手机不存在
-- const ERROR_PARAM_INVAILD = -124; //参数错误

local tb_err = {
    [-113] = error_code.VERIFY_CODE_EXCEPT,
    [-114] = error_code.VERIFY_CODE_EXCEPT,
    [-118] = error_code.GET_LIMIT_ERR,
    [-119] = error_code.LOGIN_LIMIT_ERR,
    [-120] = error_code.REGISTER_LIMIT_ERR,
    [-121] = error_code.ACCOUNT_OR_PWD_ERR,
    [-122] = error_code.PWD_ERR,
    [-123] = error_code.PHONE_ERR,
    [-124] = error_code.PARAM_ERR,
}

--帐号注册
function account.REQ_REGISTER_ACCOUNT(msg)
    local account = msg.account
    local password = msg.password
    local package_channel = msg.package_channel
    local matchine_token = msg.matchine_token
    local os_system = msg.os_system
    local platform = msg.platform
    local code = msg.code
    local body = cjson.encode({
        phone_number = account,
        pwd = password,
        client_ip = client_ip,
        package_channel = package_channel,
        matchine_token = matchine_token,
        code = code,
        os_system = os_system,
        platform = platform,
    })
    print("______________body______", body)
    local exheader = {
        ip = client_ip_num,
        channel = package_channel,
        os_system = os_system,
        platform = platform,
    }  
    local status, body = skynet.call('.web_proxy', 'lua', 'web_post', constant.REGISTER_ACCOUNT_URL, body, exheader)
    if status ~= 200 then
        return utils.send_to_gateway(0, -1, client_fd, 'account.RSP_REGISTER_ACCOUNT', {result = error_code.REGISTER_ACCOUNT_FAIL})
    end
    local result = cjson.decode(body)
    --dbglog(tostring_r(result))    
    if result.code ~= 0 then
        --ip或者机器码被限制了
        if tb_err[result.code] then
            print("---> ip or matchine limit ", result.code)
            return utils.send_to_gateway(0,-1,client_fd,'account.RSP_REGISTER_ACCOUNT',
                {result = tb_err[result.code] })
        else
            return utils.send_to_gateway(0,-1,client_fd,'account.RSP_REGISTER_ACCOUNT',
                {result = error_code.REGISTER_ACCOUNT_FAIL})
        end
    end
    if not result.account_id or not result.key_token then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_REGISTER_ACCOUNT',
            {result = error_code.REGISTER_ACCOUNT_FAIL})
    end
    --dbglog(tostring_r(result))
    local account_id = result.account_id
    local uid = skynet.call('.guid_mgr', 'lua', 'get_uid', account_id)
    if not uid then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_REGISTER_ACCOUNT',{result = error_code.REGISTER_ACCOUNT_FAIL})
    end

    billlog({op="player_bind_phone",uid = uid,phone = account,})        

    return utils.send_to_gateway(0,-1,client_fd,'account.RSP_REGISTER_ACCOUNT',{
            uid = uid,
            key_token = result.key_token,
            real_channel = result.pkg_channel,
            url = result.url,
            account = account,
        },
        uid,account_id)
end

--帐号登录
function account.REQ_LOGIN_ACCOUNT(msg)
    local account = msg.account
    local password = msg.password
    local package_channel = msg.package_channel
    local matchine_token = msg.matchine_token
    local os_system = msg.os_system
    local platform = msg.platform
    local body = cjson.encode({
            phone_number = account,
            pwd = password,
            client_ip = client_ip,
            package_channel = package_channel,
            matchine_token = matchine_token,
            os_system = os_system,
            platform = platform,
        })
    local exheader = {
        ip = client_ip_num,
        channel = package_channel,
        os_system = os_system,
        platform = platform,
    }  
    local status, body = skynet.call('.web_proxy', 'lua', 'web_post', constant.LOGIN_ACCOUNT_URL, body, exheader)
    if status ~= 200 then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_LOGIN_ACCOUNT',
            {result = error_code.LOGIN_ACCOUNT_FAIL})
    end
    local result = cjson.decode(body)
    if result.code ~= 0 then
        --ip或者机器码被限制了
        if tb_err[result.code] then
            print("---> ip or matchine limit ", result.code)
            return utils.send_to_gateway(0,-1,client_fd,'account.RSP_REGISTER_ACCOUNT',
                {result = tb_err[result.code] })
        else
            return utils.send_to_gateway(0,-1,client_fd,'account.RSP_LOGIN_ACCOUNT',
                {result = error_code.LOGIN_ACCOUNT_FAIL})
        end
    end
    local account_id = result.account_id 
    local uid = skynet.call('.guid_mgr','lua','get_uid',account_id)
    if not uid or not result.key_token then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_LOGIN_ACCOUNT',
            {result = -3})
    end
    dbglog("REQ_LOGIN_ACCOUNT",result.bind,uid)
    -- if result.bind and uid then
    --     billlog({op="player_bind_phone",uid = uid,phone = account})    
    -- end
    return utils.send_to_gateway(0,-1,client_fd,'account.RSP_LOGIN_ACCOUNT',
        {
            uid = uid,
            key_token = result.key_token,
            tourist_key_token = result.tourist_key_token,
            url = result.url,
            real_channel = result.pkg_channel,
            check_way = check_way,
            account = account,
        },
        uid,
        account_id)
end

--忘记密码
function account.REQ_FORGET_PW(msg)
    local account = msg.account
    local password = msg.password
    local code = msg.code
    local package_channel = msg.package_channel
    local matchine_token = msg.matchine_token
    local body = cjson.encode({
            phone_number = account,
            pwd = password,
            code = code,
            package_channel = package_channel,
            matchine_token = matchine_token,
            type = 1,
        })
    local exheader = {
        ip = client_ip_num,
        channel = package_channel,
    }  

    local status, body = skynet.call('.web_proxy', 'lua', 'web_post', constant.FORGET_PW_URL, body, exheader)
    if status ~= 200 then
        errlog("____REQ_FORGET_PW____", status, body)
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_FORGET_PW',
            {result = error_code.FORGET_PW_FAIL})
    end
    local result = cjson.decode(body)
    if result.code ~= 0 then
        --ip或者机器码被限制了
        if tb_err[result.code] then
            print("---> ip or matchine limit ", result.code)
            return utils.send_to_gateway(0,-1,client_fd,'account.RSP_REGISTER_ACCOUNT',
                {result = tb_err[result.code] })
        else
            return utils.send_to_gateway(0,-1,client_fd,'account.RSP_FORGET_PW',
                {result = error_code.FORGET_PW_FAIL})
        end
    end

    return utils.send_to_gateway(0, -1, client_fd, 'account.RSP_FORGET_PW', {result = 0})   

end

--修改密码
function account.REQ_CHANGE_PW(msg)
    local account = msg.account
    local password = msg.password
    local new_password = msg.new_password
    local package_channel = msg.package_channel
    local matchine_token = msg.matchine_token    
    local body = cjson.encode({
            phone_number = account,
            old_pwd = password,
            pwd = new_password,
            package_channel = package_channel,
            matchine_token = matchine_token,
            client_ip = client_ip,           
            type = 2,
        })
    local exheader = {
        ip = client_ip_num,
        channel = package_channel,
    }  

    local status, body = skynet.call('.web_proxy', 'lua', 'web_post', constant.CHANGE_PW_URL, body, exheader)
    if status ~= 200 then
        return utils.send_to_gateway(0,-1,client_fd,'account.RSP_CHANGE_PW',
            {result = error_code.CHANGE_PW_FAIL})
    end
    local result = cjson.decode(body)
    if result.code ~= 0 then
        --ip或者机器码被限制了
        if tb_err[result.code] then
            print("---> ip or matchine limit ", result.code)
            return utils.send_to_gateway(0,-1,client_fd,'account.RSP_REGISTER_ACCOUNT',
                {result = tb_err[result.code] })
        else
            return utils.send_to_gateway(0,-1,client_fd,'account.RSP_CHANGE_PW',
                {result = error_code.CHANGE_PW_FAIL})
        end
    end

    return utils.send_to_gateway(0, -1, client_fd, 'account.RSP_CHANGE_PW', {result = 0})  
end






---------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------

local handler = {
    account = account
}

local function get_msg_module_name(msgid)
    print("msgid",msgid)
    local m = msgdef.id_to_name[msgid]
    if not m then return end
    return m[1],m[2] --[1]->module,[2]->name
end

local function handler_message(...)
    local seq,msgid,msg,sz = ...
    local module,name = get_msg_module_name(msgid)
    if not module or not name then
        print('invalid msgid',msgid,module,name)
        return
    end

    local pbname = module .. '.' .. name
    local req_msg = pb.decode(pbname,msg,sz)

    print(string.format('[%s]>>>>>got a request[%s] content(%s)',
        skynet.address(skynet.self()),module .. '.' ..name,tostring_r(req_msg)))

    local module_table = handler[module]
    if not module_table or not module_table[name] then
        skynet.error(string.format('unknown requrest(%s.%s)',module,name))
        return
    end

    return module_table[name](req_msg)
end

local function dispatch(_, _,  ...)
    --就处理一次，处理完毕完则退出，非常简单暴力的一个服务
    local ret,msg = xpcall(handler_message,debug.traceback,...)
    if not ret then
        errlog(msg,...)
    end
    skynet.exit()
end

skynet.register_protocol {
    name = "client",
    id = skynet.PTYPE_CLIENT,
    unpack = function (msg, sz)
        return proxypack.unpack_client_message(msg,sz)
    end,
    dispatch = dispatch
}


local CMD = {}

function CMD.start(fd, ip)
    client_fd = fd
    client_ip_num = ip
    client_ip = util.inet_ntoa(ip)
    print("CMD.start",client_ip,fd,ip)
    skynet.retpack(true)
end

skynet.start(function()
    skynet.dispatch("lua", function(_,_, action, ...)
        local f = CMD[action]
        f(...)
    end)
    
    -- sharedata.query("global_configs")

    -- global_configs = setmetatable({},{
    --     __index = function(t,k) 
    --         return sharedata.query("global_configs")[k]
    --     end
    -- })
    global_configs = setmetatable({},{
            __index = function(t,k) 
                --return sharedata.query("global_configs")[k]
                return sharedata.query(k)
            end
        })
    _ENV.global_configs = global_configs
    _ENV.send_to_gateway = send_to_gateway
    _ENV.player = nil
    
end)
